package com.example.springbootdemo.scheduler;

import com.example.springbootdemo.bean.QuartzJobProperties;
import com.example.springbootdemo.job.Job1;
import com.example.springbootdemo.job.Job2;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
import java.util.Objects;

/**
 * 任务调度处理
 * @author hh
 * @date 2022/1/25
 */
@Slf4j
@Configuration
public class QuartzJobScheduler {

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private QuartzJobProperties quartzJobProperties;


    /**
     * 开始执行所有的任务
     * @throws SchedulerException
     */
    public void startAllJob() throws SchedulerException {
        // startJob1();
        // startJob2();
        // TODO 如果有其它任务，往下添加

        // scheduler.start();
    }

    /**
     * 任务1
     * @throws SchedulerException
     */
    private void startJob1() throws SchedulerException {
        log.info("获取到的job1表达式为：{}", quartzJobProperties.getJob1());
        // 通过JobBuilder构建JobDetail实例，JobDetail规定只能是实现Job接口的实例
        // JobDetail 是具体Job实例
        JobDetail jobDetail = JobBuilder.newJob(Job1.class)
                .withIdentity("job1", "group1")
                .build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartzJobProperties.getJob1());
        // CronTrigger表达式触发器 继承于Trigger
        // TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("job1", "group1")
                .withSchedule(cronScheduleBuilder)
                .build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 任务2
     * @throws SchedulerException
     */
    private void startJob2() throws SchedulerException {
        log.info("获取到的job2表达式为：{}", quartzJobProperties.getJob2());
        // 通过JobBuilder构建JobDetail实例，JobDetail规定只能是实现Job接口的实例
        // JobDetail 是具体Job实例
        JobDetail jobDetail = JobBuilder.newJob(Job2.class)
                .withIdentity("job2", "group2")
                .build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartzJobProperties.getJob2());
        // CronTrigger表达式触发器 继承于Trigger
        // TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("job2", "group2")
                .withSchedule(cronScheduleBuilder)
                .build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 获取job信息
     * @param name 名字
     * @param group 组
     * @return String
     */
    public String getJobInfo(String name, String group) throws SchedulerException {
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        return String.format("time:%s,state:%s", cronTrigger.getCronExpression(),
                scheduler.getTriggerState(triggerKey).name());
    }

    /**
     * 修改某个定时任务的时间
     * @param name 名字
     * @param group 组
     * @param time 时间
     * @return boolean
     */
    public boolean modifyJob(String name, String group, String time) throws SchedulerException {
        Date date = null;
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        String oldTime = cronTrigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(time)){
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time);
            CronTrigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(name, group)
                    .withSchedule(cronScheduleBuilder)
                    .build();
            date = scheduler.rescheduleJob(triggerKey, trigger);
        }
        return Objects.nonNull(date);
    }

    /**
     * 暂停所有任务
     */
    public void pauseAllJob() throws SchedulerException {
        scheduler.pauseAll();
    }

    /**
     * 暂停某个任务
     * @param name 名字
     * @param group 组
     * @return boolean
     */
    public boolean pauseJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (Objects.isNull(jobDetail)){
            return false;
        }
        scheduler.pauseJob(jobKey);
        return true;
    }

    /**
     * 恢复所有任务
     */
    public void resumeAllJob() throws SchedulerException {
        scheduler.resumeAll();
    }

    /**
     * 恢复某个任务
     * @param name 名字
     * @param group 组
     * @return boolean
     */
    public boolean resumeJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (Objects.isNull(jobDetail)){
            return false;
        }
        scheduler.resumeJob(jobKey);
        return true;
    }

    /**
     * 删除某个任务
     * @param name 名字
     * @param group 组
     * @return boolean
     */
    public boolean deleteJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (Objects.isNull(jobDetail)){
            return false;
        }
        return scheduler.deleteJob(jobKey);
    }

}
